package com.js.admin.mapper;

import com.js.admin.entity.BaseMenu;
import com.js.common.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author ygl
 * @create 2018-10-31
 * @DESCRIPTION
 **/
public interface BaseMenuMapper extends BaseMapper<BaseMenu> {
    /**
     * 根据资源类型查询菜单
     * @param authorityId
     * @param authorityType
     * @param type
     * @return
     */
    @Select(" select t.* from base_resource_authority ra inner join base_menu t " +
            " on ra.resource_id = t.id and ra.authority_id = #{authorityId} " +
            " and ra.authority_type = #{authorityType} and ra.resource_type = 'menu' and ra.type = #{type}")
    List<BaseMenu> selectMenuByAuthorityId(@Param("authorityId") String authorityId, @Param("authorityType") String authorityType, @Param("type") String type);

    /**
     * 查找租户可访问的权限菜单
     * @param appId
     * @param type
     * @return
     */
    @Select(" select distinct t.* from base_resource_authority ra inner join base_menu t " +
            " on ra.resource_id = t.id and ra.authority_id in " +
            " ( select group_id from base_app_group where app_id = #{appId}) " +
            " and ra.authority_type = 'group' and ra.resource_type = 'menu' and ra.type = #{type} " +
            " order by t.id")
    List<BaseMenu> selectAuthorityMenuByAppId(@Param("appId") String appId, @Param("type") String type);

    /**
     * 根据用户和组的权限关系查找用户可访问菜单
     *
     * @param userId
     * @param type
     * @return
     */
    @Select(" select distinct t.* from base_resource_authority ra inner join base_menu t " +
            " on ra.resource_id = t.id and ra.authority_id in " +
            " (select group_id from base_group_member where user_id = #{userId} " +
            "  union select group_id from base_group_leader where user_id = #{userId} " +
            "  union select bpg.group_id from base_position_group bpg inner join base_position_user bpu " +
            "        on bpg.position_id = bpu.position_id and bpu.user_id = #{userId}) " +
            " and ra.authority_type = 'group' and ra.resource_type = 'menu' and ra.type = #{type} " +
            " order by t.id")
    List<BaseMenu> selectAuthorityMenuByUserId(@Param("userId") String userId, @Param("type") String type);

}
